Cross-Compilation
With compilation, we refer to the act of translating 1 language to another. A high level (source code) language to another that is then able to execute on a given CPU architecture.
Source Code --> Compilation --> Binary
This can be a complex process of multiple stages, the general idea, however, is binaries are compiled for specific environments. Environments are
- Hardware architecture
- OS
- Other user-space program and configurations
The term cross compilation is a technique used when we want to compile, not for our current environment but for our target one.
An example
ELF -> PE
Linux Executable and Linkable Format -> Windows Portable Executable
GCC will compile C code to create an elf
gcc hello.c -o hello
Cross compile with Windows
sudo pacman -S mingw-w64-gcc
Also try without static
x86_64-w64-mingw64-g++ hello.c -static -o hello
For 32-bit
i686-w64-mingw32-gcc
If programs get more complex, you may need to cross-compile across Windows libraries. Note the actual output will be shell.exe. Static binary doesn't depend on Windows libraries.
x86_64-w64-mingw64-g++ shell.c -static -lws2_32 -o shell
A reverse shell script is available under ~/Downloads/Windows/shell.c.
Make sure to modify it with the following settings
- Attacker IP
- Attacker port
x86_64-w64-mingw64-g++ ~/Downloads/Windows/shell.c -static -lws2_32 -o shell
14.1.3 - Cross-compiling exploit code
Use mingw64 to compile code into a Windows PE executable (in this example)
sudo apt install mingw-64
i686-w64-mingw32-gcc 42341.c -o syncbreeze_exploit.exe
x86_64-w64-mingw32-gcc -mwindows -municode -O2 -s -o simpleService.exe simpleService.s
Example using the winsock library should compilation fail without this specific library. This example targets 32 bit Windows.
i686-w64-mingw32-gcc 42341.c -o syncbreeze_exploit.exe -lws2_32
A separate example utilizing 64 bit Windows
x86_64-w64-mingw32-g++ Sincro.cpp --shared -o Sincro.dll
x86_64-w64-mingw32-gcc TextShaping.cpp -shared -o TextShaping.dll